package de.fhkl.thatsstroke.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Database connection helper takes care of opening the database if it exists,
 * creating it if it does not, and upgrading it as necessary. </br> </br>
 * <strong>Note:</strong> this class assumes monotonically increasing version
 * numbers for upgrades.
 */
public class StrokeSQLiteHelper extends SQLiteOpenHelper
{
	/** Database table for videos. */
	public static final String TABLE_VIDEOS = "videos";

	// database columns
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_VIDEO_ID = "video_id";
	public static final String COLUMN_DATE = "date";
	public static final String COLUMN_FLAG = "flag";
	public static final String COLUMN_PARTS = "parts";
	public static final String COLUMN_PROGRESS = "progress";

	/** Name of the database file on the device. Must end with ".db" . */
	private static final String DATABASE_NAME = "videos.db";

	/**
	 * Version of the database. Increment to signal the onUpgrade method that
	 * the database must be dropped and recreated
	 */
	private static final int DATABASE_VERSION = 10;

	/** Create statement for the video table */
	private static final String DATABASE_CREATE = "create table " + TABLE_VIDEOS + "(" + COLUMN_ID
			+ " integer primary key autoincrement, " + COLUMN_VIDEO_ID + " text not null unique, " + COLUMN_DATE
			+ " integer not null, " + COLUMN_FLAG + " integer not null, " + COLUMN_PARTS + " integer not null, "
			+ COLUMN_PROGRESS + " integer);";

	/**
	 * Constructs a new StrokeSQLiteHelper.
	 * 
	 * @param context
	 */
	public StrokeSQLiteHelper(Context context)
	{
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db)
	{
		db.execSQL(DATABASE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		Log.i("StrokeSQLiteHelper", "Upgrading database from version " + oldVersion + " to " + newVersion);

		db.execSQL("DROP TABLE IF EXISTS " + TABLE_VIDEOS);
		onCreate(db);
	}
}
